package com.easychat.controller;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.entity.dto.TokenUserInfoDto;
import com.easychat.entity.dto.UserContactSearchResultDto;
import com.easychat.entity.enums.PageSize;
import com.easychat.entity.enums.ResponseCodeEnum;
import com.easychat.entity.enums.UserContactStatusEnum;
import com.easychat.entity.enums.UserContactTypeEnum;
import com.easychat.entity.po.UserContact;
import com.easychat.entity.po.UserContactApply;
import com.easychat.entity.po.UserInfo;
import com.easychat.entity.query.UserContactApplyQuery;
import com.easychat.entity.query.UserContactQuery;
import com.easychat.entity.query.UserInfoQuery;
import com.easychat.entity.vo.PaginationResultVO;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.entity.vo.UserInfoVO;
import com.easychat.exception.BusinessException;
import com.easychat.service.UserContactApplyService;
import com.easychat.service.UserContactService;
import com.easychat.service.UserInfoService;
import com.easychat.utils.CopyTools;
import jodd.util.ArraysUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @program: easychat-java
 * @description:
 * @author: Mr.Wang
 * @create: 2024-12-02 17:26
 **/
@RestController("userContactController")
@RequestMapping("contact")
public class UserContactController extends ABaseController {

    @Resource
    private UserContactService userContactService;

    @Resource
    private UserContactApplyService userContactApplyService;
    @Resource
    private UserInfoService userInfoService;

    /**
     * 搜索用户或群组
     *
     * @param request
     * @param contactId
     * @return
     */
    @GlobalInterceptor
    @RequestMapping("search")
    public ResponseVO search(HttpServletRequest request, @NotEmpty String contactId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto(request);
        UserContactSearchResultDto resultDto = userContactService.searchContact(tokenUserInfoDto.getUserId(), contactId);
        return getSuccessResponseVO(resultDto);
    }

    /**
     * 申请添加好友
     *
     * @param request
     * @param contactId
     * @param contactType
     * @param applyInfo
     * @return
     */
    @GlobalInterceptor
    @RequestMapping("applyAdd")
    public ResponseVO applyAdd(HttpServletRequest request, @NotEmpty String contactId,
                               @NotEmpty String contactType, String applyInfo) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto(request);
        Integer joinType = userContactApplyService.applyAdd(tokenUserInfoDto, contactId, contactType, applyInfo);
        return getSuccessResponseVO(joinType);
    }

    /**
     * 分页查询申请加好友列表
     *
     * @param request
     * @param pageNo
     * @return
     */
    @GlobalInterceptor
    @RequestMapping("loadApply")
    public ResponseVO loadApply(HttpServletRequest request, Integer pageNo) {
        TokenUserInfoDto userInfoDto = getTokenUserInfoDto(request);
        UserContactApplyQuery userContactApplyQuery = new UserContactApplyQuery();
        userContactApplyQuery.setReceiveUserId(userInfoDto.getUserId());
        userContactApplyQuery.setOrderBy("last_apply_time desc");
        userContactApplyQuery.setPageSize(PageSize.SIZE15.getSize());
        userContactApplyQuery.setPageNo(pageNo);
        //mybatis动态sql分页查询条件
        userContactApplyQuery.setQueryContactInfo(true);
        PaginationResultVO<UserContactApply> resultVO = userContactApplyService.findListByPage(userContactApplyQuery);
        return getSuccessResponseVO(resultVO);
    }

    /**
     * 处理好友申请
     *
     * @param request
     * @param applyId
     * @param status
     * @return
     */
    @GlobalInterceptor
    @RequestMapping("dealWithApply")
    public ResponseVO dealWithApply(HttpServletRequest request, @NotNull Integer applyId, @NotNull Integer status) {
        TokenUserInfoDto userInfoDto = getTokenUserInfoDto(request);
        userContactApplyService.dealWithApply(userInfoDto.getUserId(), applyId, status);
        return getSuccessResponseVO(null);
    }

    /**
     * 获取联系人列表
     *
     * @param request
     * @param contactType
     * @return
     */
    @GlobalInterceptor
    @RequestMapping("loadContact")
    public ResponseVO loadContact(HttpServletRequest request, @NotEmpty String contactType) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto(request);
        UserContactTypeEnum typeEnum = UserContactTypeEnum.getByName(contactType);
        if (typeEnum == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        UserContactQuery userContactQuery = new UserContactQuery();
        userContactQuery.setUserId(tokenUserInfoDto.getUserId());
        userContactQuery.setContactType(typeEnum.getType());
        //区分 人还是群组
        if (typeEnum == UserContactTypeEnum.USER) {
            //连接用户表查询动态sql条件
            userContactQuery.setQueryContactUserInfo(true);
        } else if (typeEnum == UserContactTypeEnum.GROUP) {
            //连接群组表查询动态sql条件
            userContactQuery.setQueryGroupInfo(true);
            //排除自己创建的群组
            userContactQuery.setExcludeMyGroup(true);

        }
        //可被查询的好友状态
        userContactQuery.setStatusArray(new Integer[]{
                UserContactStatusEnum.FRIEND.getStatus(),
                UserContactStatusEnum.DEL_BE.getStatus(),
                UserContactStatusEnum.BLACKLIST_BE.getStatus()
        });
        userContactQuery.setOrderBy("last_update_time desc");
        List<UserContact> listByParam = userContactService.findListByParam(userContactQuery);
        return getSuccessResponseVO(listByParam);
    }

    /**
     * 获取他人信息
     * @param request
     * @param contactId
     * @return
     */
    @GlobalInterceptor
    @RequestMapping("getContactInfo")
    public ResponseVO getContactInfo(HttpServletRequest request, @NotEmpty String contactId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto(request);
        UserInfo userInfo = userInfoService.getUserInfoByUserId(contactId);
        UserInfoVO userInfoVO = CopyTools.copy(userInfo, UserInfoVO.class);
        //默认给非好友状态
        userInfoVO.setContactStatus(UserContactStatusEnum.NOT_FRIEND.getStatus());
        UserContact userContact = userContactService.getUserContactByUserIdAndContactId(tokenUserInfoDto.getUserId(), contactId);
        if (userContact!=null){
            //如果是联系人给表中状态
            userInfoVO.setContactStatus(userContact.getStatus());
        }
        return getSuccessResponseVO(userInfoVO);
    }

    /**
     * 获取联系人信息
     * @param request
     * @param contactId
     * @return
     */
    @GlobalInterceptor
    @RequestMapping("getContactUserInfo")
    public ResponseVO getContactUserInfo(HttpServletRequest request, @NotEmpty String contactId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto(request);
        UserContact userContact = userContactService.getUserContactByUserIdAndContactId(tokenUserInfoDto.getUserId(), contactId);
        if (userContact==null|| !ArraysUtil.contains(new Integer[]{
                UserContactStatusEnum.FRIEND.getStatus(),
                UserContactStatusEnum.DEL_BE.getStatus(),
                UserContactStatusEnum.BLACKLIST_BE.getStatus(),
                UserContactStatusEnum.BLACKLIST_BE_FIRST.getStatus()},userContact.getStatus())){
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }

        UserInfo userInfo = userInfoService.getUserInfoByUserId(contactId);
        UserInfoVO userInfoVO = CopyTools.copy(userInfo, UserInfoVO.class);
        return getSuccessResponseVO(userInfoVO);
    }

    /**
     * 删除联系人
     * @param request
     * @param contactId
     * @return
     */
    @GlobalInterceptor
    @RequestMapping("delContact")
    public ResponseVO delContact(HttpServletRequest request, @NotEmpty String contactId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto(request);
      userContactService.removeUserContact(tokenUserInfoDto.getUserId(), contactId,UserContactStatusEnum.DEL);
        return getSuccessResponseVO(null);
    }

    /**
     * 添加到黑名单
     * @param request
     * @param contactId
     * @return
     */
    @GlobalInterceptor
    @RequestMapping("addContact2BlackList")
    public ResponseVO addContact2BlackList(HttpServletRequest request, @NotEmpty String contactId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto(request);
        userContactService.removeUserContact(tokenUserInfoDto.getUserId(), contactId,UserContactStatusEnum.BLACKLIST);
        return getSuccessResponseVO(null);
    }

}
